﻿Imports System.Data.SqlClient

Public Class Frm_NhaCungCap
    Public clsdm As New Cls_DanhMuc

    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
         If Me.btthem.Text = "&Thêm" Then
            Me.btthem.Text = "&Hủy"
            Me.btluu.Enabled = True
            Me.btsua.Enabled = False
            Me.btxoa.Enabled = False
            Me.txttenncc.Text = ""
            Me.txttenncc.Focus()

        Else
            Me.btthem.Text = "&Thêm"
            Me.txtmancc.Text = ""
            Me.btsua.Enabled = True
            Me.btxoa.Enabled = True

        End If
    End Sub

    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        'If KiemTraTextBox() = 1 Then
        Dim cmd As New SqlCommand("ThemNCC", con)
            cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Tenncc", SqlDbType.NVarChar).Value = txttenncc.Text.Trim
        cmd.Parameters.Add("@Diachi", SqlDbType.NVarChar).Value = txtdiachi.Text.Trim
        cmd.Parameters.Add("@Dienthoai", SqlDbType.NVarChar).Value = txtdienthoai.Text.Trim
            Try
                cmd.ExecuteNonQuery()
                MsgBox("Cập nhật thành công ", MsgBoxStyle.Information, "Thông Báo")
            FillData()
            Me.txttenncc.Text = ""
            Catch ex As Exception
                MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
            End Try
       ' End If
        Me.btthem.Text = "&Thêm"
        Me.btluu.Enabled = False
        Me.btsua.Enabled = True
        Me.btxoa.Enabled = True

    End Sub

    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        Dim strma, strten As String
        strma = dtg.CurrentRow.Cells(0).Value.ToString()
        strten = dtg.CurrentRow.Cells(1).Value.ToString()
        If MsgBox("Bạn có chắc xóa Nhà Cung Cấp : " & strten & "?", MsgBoxStyle.YesNo, "Thông Báo") = MsgBoxResult.Yes Then
            Try
                Dim cmd As New SqlCommand("XoaNCC", con)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@MaNCC", SqlDbType.Int).Value = Me.txtmancc.Text.Trim
                cmd.ExecuteNonQuery()
                FillData()
                Me.txttenncc.Text = ""
            Catch ex As Exception
                MsgBox("Lỗi khi xóa" & ex.Message)
                Exit Sub
            End Try

        End If


        ' End If
    End Sub

    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        Dim cmd As New SqlCommand("SuaNCC", con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Mancc", SqlDbType.Int).Value = txtmancc.Text.Trim
        cmd.Parameters.Add("@Tenncc", SqlDbType.NVarChar).Value = txttenncc.Text.Trim
        cmd.Parameters.Add("@Diachi", SqlDbType.NVarChar).Value = txtdiachi.Text.Trim
        cmd.Parameters.Add("@Dienthoai", SqlDbType.NVarChar).Value = txtdienthoai.Text.Trim
        Try
            cmd.ExecuteNonQuery()
            MsgBox("Cập nhật thành công ", MsgBoxStyle.Information, "Thông Báo")
            FillData()
            Me.txttenncc.Text = ""
        Catch ex As Exception
            MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
        End Try
    End Sub
    Private Sub btthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthoat.Click
        Me.Close()
    End Sub
    Private Sub Frm_NhaCungCap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillData()
    End Sub
    Private Sub FillData()
        clsdm.FillDataGirdView("select * from Nhacungcap", Me.dtg, Me.dts)
    End Sub
End Class